require(["common", "coAjax", "queryString", "Cookies", "href", "hash_routie", "eventManager"], function (jSouper, coAjax, QueryString, Cookies, href, hash_routie, eventManager) {
	App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
	var queryString;
	hash_routie.on("sign", function () {
		queryString = new QueryString(location.hash);
		if (queryString.get("id") == "2") {
			console.log("hehe");
			App.set("is_sign_up", true);
		} else {
			App.set("is_sign_up", false);
		}
	});
	// 登陆注册切换
	App.set("$Event.changToSign_in", function () {
		App.set("is_sign_up", false);
	});
	App.set("$Event.changToSign_up", function () {
		App.set("is_sign_up", true);
	});
	
	//获取一个验证码
	function _loadCodeImg() {
		// $code.val("");
		// $signInImg.prop("src",appConfig.user.sign_in_img_code_url+"?_="+Math.random());
	};
	_loadCodeImg();
	// 登陆
	App.set("$Event.sign_in.login", function () {
		var login_data = App.get("$Cache.sign_in");
		coAjax.post(appConfig.user.sign_in_url, login_data, function success(result) {
			alert("success", "登录成功");
			App.set("loginer", result.result);
			Cookies.set("cache_user_login_name", login_data.name);
			Cookies.set("loginer", result.result);
			var callbackUrl = queryString.get("cb_url");
			if (callbackUrl) {
				href.jump(callbackUrl);
			} else {
				//到首页
				href.jump("catering.main.html#default/main");
			}
			eventManager.fire("getLoginer");
		}, function error(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				_loadCodeImg();
			});
	});


	/*
	 * 注册
	 */
	App.set("$Event.sign_up.register", function () {
		if (!App.get('$Cache.sign_up.name')) {
			alert('warn', '请输入用户名')
			return;
		};
		if (!App.get('$Cache.sign_up.mobile_phone')) {
			alert('warn', '请输入手机号')
			return;
		};
		if (!App.get('$Cache.sign_up.email')) {
			alert('warn', '请输入邮箱')
			return;
		};
		if (!App.get('$Cache.sign_up.password')) {
			alert('warn', '请输入密码')
			return;
		};
		if (!App.get('$Cache.sign_up.confirm_password')) {
			alert('warn', '请再次输入密码')
			return;
		};
		if (!App.get('$Cache.sign_up.phone_code')) {
			alert('warn', '请输入验证码')
			return;
		};
		var register_data = App.get("$Cache.sign_up");
		coAjax.post(appConfig.user.sign_up_url, register_data, function (result) {
			alert("success", "注册成功");
			App.set("$Cache.sign_in.name", register_data.name);
			//清空表单
			App.set("$Cache.sign_up", {});
			//切换到登录
			App.get("$Event.sign_up.to_sign_in")();
		}, function (errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				//重新获取验证码
				App.set("$Cache.sign_up.wite_server", false);
			});

	});
	var _ti;

	function _phone_code_time(argument) {
		clearInterval(_ti);
		var _s = 60;
		_ti = setInterval(function () {
			_s -= 1;
			App.set("$Cache.sign_up.next_phone_code_time", _s);
			if (_s === 0) {
				clearInterval(_ti);
				App.set("$Cache.sign_up.wite_server", false);
			}
		}, 1000);
	}
	App.set("$Event.sign_up.get_register_code", function () {
		App.set("$Cache.sign_up.wite_server", true);
		coAjax.get(appConfig.user.register_code, {
			mobile_phone: App.get("$Cache.sign_up.mobile_phone")
		}, function (result) {
				alert(result.result);
				_phone_code_time();
			}, function (errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				App.set("$Cache.sign_up.wite_server", false);
			});
	});
	App.set('$Event.ApplyRule', function () {
		App.model.toggle('isApplyRules');
	});
	App.set("$Event.sign_up.checkedMobilePhone", function () {
		coAjax.get(appConfig.user.registerCheckedMobilePhone + App.get("$Cache.sign_up.mobile_phone"), function () {
			alert("success", "手机号码可用");
		});
	});
	App.set("$Event.sign_up.checkedEmail", function () {
		coAjax.get(appConfig.user.registerCheckedEmail + App.get("$Cache.sign_up.email"), function () {
			alert("success", "邮箱可用");
		});
	});
	App.set("$Event.findpaw", function () {
		href.jump("http://www.dotnar.com/find_paw.html?cb=" + location.host);
	});
});